//
//  AppleRemoteInterface.m
//  Shion
//
//  Created by Chris Karr on 12/19/07.
//  Copyright 2007-2008 Audacious Software. All rights reserved.
//

#import "AppleRemoteInterface.h"
#import "ShionAppDelegate.h"
#import "Device.h"
#import "DeviceManager.h"

@implementation AppleRemoteInterface

- (void) awakeFromNib
{
	remoteControl = [[AppleRemote alloc] initWithDelegate: self];
	[remoteControl startListening: self];
}

- (void) activate: (NSNotification *) msg
{
	[remoteControl startListening: self];
}

- (void) deactivate: (NSNotification *) msg
{
	[remoteControl stopListening: self];
	[remotePanel orderOut:self];
}


- (void) sendRemoteButtonEvent: (RemoteControlEventIdentifier) event 
                   pressedDown: (BOOL) pressedDown 
                 remoteControl: (RemoteControl*) remoteControl 
{
	
	Device * d = nil;
	NSArray * selects = [devices selectedObjects];

	if ([selects count] > 0)
		d = [selects objectAtIndex:0];
		
	if (event == kRemoteButtonPlus && !pressedDown)
		[devices selectPrevious:nil];
	else if (event == kRemoteButtonMinus && !pressedDown)
		[devices selectNext:nil];
	else if (event == kRemoteButtonMenu && !pressedDown)
	{
		if ([remotePanel isVisible])
			[remotePanel orderOut:self];
		else
		{
			NSScreen * screen = [NSScreen mainScreen];
			NSRect screenRect = [screen visibleFrame];
		
			NSRect panelRect = [remotePanel frame];
			
			NSRect windowRect = NSMakeRect (screenRect.origin.x, screenRect.origin.y, panelRect.size.width, panelRect.size.height);

			windowRect.origin.x = (screenRect.size.width - panelRect.size.width) / 2;
			windowRect.origin.y += (screenRect.size.height - panelRect.size.height) / 2;
		
			[remotePanel setFrame:windowRect display:YES];

			[remotePanel makeKeyAndOrderFront:self];
		}
	}
	else if (event == kRemoteButtonPlay && !pressedDown && d != nil)
	{
		if ([d.actualStatus.lightLevel integerValue] == 0)
			d.visibleStatus.lightLevel = [NSNumber numberWithInteger:255];
		else
			d.visibleStatus.lightLevel = [NSNumber numberWithInteger:0];
		
		[[NSNotificationCenter defaultCenter] postNotificationName:SHION_SYNCNOW object:self];
	}
	else if (event == kRemoteButtonRight && !pressedDown && d != nil)
	{
		NSInteger todo = [d.visibleStatus.lightLevel integerValue] + 32;
		
		if (todo > 255)
			todo = 255;
		
		d.visibleStatus.lightLevel = [NSNumber numberWithInteger:todo];
		[[NSNotificationCenter defaultCenter] postNotificationName:SHION_SYNCNOW object:self];
	}
	else if (event == kRemoteButtonLeft && !pressedDown && d != nil)
	{
		NSInteger todo = [d.visibleStatus.lightLevel integerValue] - 32;
		
		if (todo < 0)
			todo = 0;
		
		d.visibleStatus.lightLevel =[NSNumber numberWithInteger:todo];
		[[NSNotificationCenter defaultCenter] postNotificationName:SHION_SYNCNOW object:self];
	}
}

- (NSString *) machineName
{
	CFStringRef name;
	NSString * computerName;
	name = CSCopyMachineName ();
	computerName = [NSString stringWithString:(NSString *) name];
	CFRelease (name);

	return computerName;
}

@end
